#!/bin/sh
# by xupeng@4399inc.com time: 2019-5-25

if [ `whoami` = "root" ];then

    #安装docker
    if ! [ -x "$(command -v docker)" ]; then

        #ps: 最新版本
        #  https://www.docker.elastic.co/#

        #查看版本
        # rpm -qa | grep docker

        #卸载 rpm -e --nodeps
        #删除旧版本的docker
        yum remove -y docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine


        #centos 6
        wget https://yum.dockerproject.org/repo/main/centos/6/Packages/docker-engine-1.7.1-1.el6.x86_64.rpm

        # #centos 7
        # wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.10.0-1.el7.centos.x86_64.rpm

        # rpm -ivh docker-engine-1.10.0-1.el7.centos.x86_64.rpm --nodeps --force

        # wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.13.1-1.el7.centos.x86_64.rpm
        # rpm -ivh docker-engine-1.13.1-1.el7.centos.x86_64.rpm --nodeps --force


        yum install -y libcgroup-*
        rpm -ivh docker-engine-1.7.1-1.el6.x86_64.rpm

        #删除 rpm
        rm -rf docker-engine-1.7.1-1.el6.x86_64.rpm

        #search
        yum list installed | grep docker
        ps -ef|grep docker

        #启动 docker
        service docker start

        docker version

        #设置开机启动
        systemctl enable docker.service
    fi

    #docker-compose
    if ! [ -x "$(command -v docker-compose)" ]; then
        curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
        chmod +x /usr/local/bin/docker-compose
        docker-compose --version
    fi
else
    echo "not root"
    exit;
fi

fpack=`docker images|grep $1 |awk '{print $1}'|sed -n '1p'`

case "$1" in
redis)
    if [ ${fpack} = $1 ]; then
        echo "have redis"
    else
        echo "not redis"
        #安装
        docker pull  redis:3.2

        #启动
        docker run -p 6379:6379 -v $PWD/data:/data  -d redis:3.2 redis-server --appendonly yes
    fi
    ;;
mysql)
    if [ ${fpack} = $1 ]; then
        echo "have mysql"
    else
        echo "not mysql"
        #安装
        docker pull mysql:5.6

        #启动
        docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
    fi
    ;;
elk)
    elkfpack=`docker images|grep elasticsearch |awk '{print $1}'|sed -n '1p'`

    if [[ ${elkfpack} =~ "elasticsearch" ]]; then
        echo "have elk is have restart elk===>>>>>>>"
        sh $PWD/docker_setup/restart
    else
        echo "not elk setup elasticsearch + kibana + logstash version:7.1.0"
        sh $PWD/docker_setup/restart
    fi
    ;;
mongo)
    if [ ${fpack} = $1 ]; then
        echo "have mongo"
    else
        echo "not mongo"
        docker pull mongo
        docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo
    fi
    ;;
*)
    echo "no pack !!!"
    ;;
esac
https://www.docker.elastic.co/#
